unit Color;

interface

uses Mathematic, SysUtils;

type
  TColorData = array [0..3] of TFloat;
  TColorDataPointer = ^TColorData;
  TColor4f = class
  private
    aData: TColorData;
    aDataPointer: TColorDataPointer;
    procedure SetR(paValue: TFloat);
    function GetR: TFloat;
    procedure SetG(paValue: TFloat);
    function GetG: TFloat;
    procedure SetB(paValue: TFloat);
    function GetB: TFloat;
    procedure SetA(paValue: TFloat);
    function GetA: TFloat;
    procedure SetData(paData: TColorData);
    procedure SetIndexData(I:Integer; Value: TFloat);
    function GetIndexData(I:Integer): TFloat;
  public
    constructor Create;overload;
    constructor Create(paR, paG, paB, paA: TFloat);overload;
    constructor CreateCopy(paColor: TColor4f);
    destructor Destroy;override;
    procedure MakeCopy(paColor: TColor4f);
    procedure SetRGBA(paR, paG, paB, paA: TFloat);
    function SaveToFile(var F: Text): Boolean;
    function LoadFromFile(var F: Text): Boolean;
    property R: TFloat read GetR write SetR;
    property G: TFloat read GetG write SetG;
    property B: TFloat read GetB write SetB;
    property A: TFloat read GetA write SetA;
    property DataPointer: TColorDataPointer read aDataPointer write aDataPointer;
    property Value[I: Integer]: TFloat read GetIndexData write SetIndexData;default;
  end;
  TColor = TColor4f;

implementation

procedure TColor4f.SetR(paValue: TFloat);
begin
    aData[0] := paValue;
end;

function TColor4f.GetR: TFloat;
begin
    Result := aData[0];
end;

procedure TColor4f.SetG(paValue: TFloat);
begin
    aData[1] := paValue;
end;

function TColor4f.GetG: TFloat;
begin
    Result := aData[1];
end;

procedure TColor4f.SetB(paValue: TFloat);
begin
    aData[2] := paValue;
end;

function TColor4f.GetB: TFloat;
begin
    Result := aData[2];
end;

procedure TColor4f.SetA(paValue: TFloat);
begin
    aData[3] := paValue;
end;

function TColor4f.GetA: TFloat;
begin
    Result := aData[3];
end;

procedure TColor4f.SetData(paData: TColorData);
var I: Integer;
begin
    for I := 0 to 3 do begin
        aData[I] := paData[I];
    end;
end;

procedure TColor4f.SetIndexData(I: Integer; Value: TFloat);
begin
    aData[I] := Value;
end;

function TColor4f.GetIndexData(I: Integer): TFloat;
begin
    Result := aData[I];
end;

constructor TColor4f.Create;
begin
    inherited Create;
    R := 0.0;
    G := 0.0;
    B := 0.0;
    A := 0.0;
    aDataPointer := @aData;
end;

constructor TColor4f.Create(paR, paG, paB, paA: TFloat);
begin
    inherited Create;
    R := paR;
    G := paG;
    B := paB;
    A := paA;
    aDataPointer := @aData;
end;

constructor TColor4f.CreateCopy(paColor: TColor4f);
begin
    inherited Create;
    SetData(paColor.aData);
    aDataPointer := @aData;
end;

destructor TColor4f.Destroy;
begin
    inherited Destroy;
end;

procedure TColor4f.MakeCopy(paColor: TColor4f);
begin
    SetData(paColor.aData);
    aDataPointer := @aData;
end;

procedure TColor4f.SetRGBA(paR, paG, paB, paA: TFloat);
begin
    R := paR;
    G := paG;
    B := paB;
    A := paA;
end;

function TColor4f.SaveToFile(var F: Text): Boolean;
begin
    FloatToFile(R, F);
    FloatToFile(G, F);
    FloatToFile(B, F);
    FloatToFile(A, F);
end;

function TColor4f.LoadFromFile(var F: Text): Boolean;
begin
    FloatFromFile(aData[0], F);
    FloatFromFile(aData[1], F);
    FloatFromFile(aData[2], F);
    FloatFromFile(aData[3], F);
end;

end.
